﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frames;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace Inference
{
    public static class SlotFrameFuncs
    {
        public static void DeleteSlot(int slotID,TaleEntities tale)
        {
            int success = 0;
            if (slotID >= 0)
            {
                SqlCommand get_facts, delete_slot;
                //get_facts = new SqlCommand("", conn);
                //get_facts.CommandText = "select * from Fact where SlotID=" + slotID;
                //SqlDataAdapter sadapt = new SqlDataAdapter(get_facts);
                //DataTable df = new DataTable();
                //sadapt.Fill(df);
                var facts = tale.Fact.Where(v => v.SlotID == slotID);
                foreach (Fact fct in facts)
                {
                    try
                    {
                        var prod_fact = tale.ProductionFact.Where(v => v.FactID == fct.ID).First();
                        tale.ProductionFact.DeleteObject(prod_fact);
                        tale.SaveChanges();                        
                    }
                    catch { success = -1; }
                }
                foreach (Fact fct in facts)
                {
                    try
                    {
                        tale.Fact.DeleteObject(fct);
                        tale.SaveChanges();                        
                    }
                    catch { success = -1; }
                }
                TaleEntities tale1 = new TaleEntities();
                var slot = tale1.Slot.Where(v => v.ID == slotID);
                //if (slot!=null)                
                try
                {
                    tale1.Slot.DeleteObject(slot.First());
                    tale1.SaveChanges();         
                }
                catch { success = -1; }
            }
        }

        public static void ReindexSlots(int frameID)//, TaleEntities Tale)   // numbers
        {
            TaleEntities Tale = new TaleEntities();
            var slots = Tale.Slot.Where(v => v.FrameID == frameID);
            int i = 0;
            foreach (Inference.Slot sl in slots)
            {
                try
                {
                    sl.Number = i;
                    Tale.SaveChanges();
                }
                catch { }
                i++;
            }
        }
    }
}
